import { ref } from 'vue'
import { defineStore } from 'pinia'
import router from '@/router/index.js'
import { sessionSet } from '@/utils/sessionStorage'
import { roleMenuApi } from '@/api/component/menu/menu'
interface Role {
  [key: string]: string;
}
export interface RouterItem {
  name: string;
  icon: string;
  role: Role;
}

export interface Router {
  [key: string]: RouterItem;
}
const useCounterStore = defineStore('usersStores', () => {
  const token = ref('')
  const menuList = ref([] as Array<Router>);
  /**
   * 存储token，跳转页面
   */
  function getToken(data: string): void {
    try {
      token.value = data
      router.push('/')

      sessionSet('token', data)
    } catch (error) {
      console.log(error)
    }
  }
  /**
   *获取菜单栏列表权限，将其存储到本地
   */
  async function getMenuList(name: string): Promise<void> {
    const { router } = await roleMenuApi({ name })
    menuList.value = router || []
    sessionSet('menu', router)
  }

  return { getToken, getMenuList }
})
export default useCounterStore
